package com.hd.controller.app;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hd.commons.shiro.PasswordHash;
import com.hd.controller.base.BaseController;
import com.hd.entity.sys.User;
import com.hd.service.sys.UserService;
import com.hd.util.JsonUtil;
/**
 * app后台登录
 * @author hzhh123
 * 2018年9月4日上午11:46:45
 */
@Controller
@RequestMapping("/app")
public class AppLoginController extends BaseController{
	@Autowired
	private UserService userService;
	@Autowired
	private PasswordHash passwordHash;
	@RequestMapping("/login")
	@ResponseBody
	public Object login(String username,String password) {
		User user=new User();
		user.setUsername(username);
		List<User> users = userService.selectByLoginName(user);
		if(users.size()==0) {
			System.out.println("用户名错误！");
			return renderError("用户名错误！");
		}
		User loginUser=users.get(0);
		System.out.println(JsonUtil.toJson(loginUser));
		String pwd=passwordHash.toHex(password, loginUser.getSalt());
		if(!loginUser.getPassword().equals(pwd)) {
			return renderError("密码错误！");
		}
		System.out.println("登录成功！");
		return renderSuccess(users.get(0));
	}
}
